package com.hx.rpc;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Huathy
 * @date 2021-10-17 22:31
 * @description
 */
public class RpcProxyServer {

    @SuppressWarnings("all")
    ExecutorService executorService = Executors.newCachedThreadPool();

    public void publish(int port,Object targetService){
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            while (true){   //不断接收请求x
                Socket socket = serverSocket.accept();  //BIO
                //每一个socket，交给一个processor来处理
                executorService.execute(new ProcessorHandler(socket,targetService));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
